package com.buba.springcloud.controller;
import com.buba.springcloud.annotation.RecordOperation;
import com.buba.springcloud.entities.CommonResult;
import com.buba.springcloud.entities.HfFoods;
import com.buba.springcloud.entities.HfFoodtype;
import com.buba.springcloud.service.FoodService;
import com.buba.springcloud.util.AliyunOSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @Author taodeyu
 * @Date 2021/12/29 上午 11:25
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/backstagesupporter/food")
public class FoodController {
    @Resource
    private FoodService foodService;


    @Resource
    private AliyunOSSUtil aliyunOSSUtil;
    /**
     * 食物列表全查(包含模糊查询,可以根据类型状态等进行查询)
     * @param foodName
     * @param foodType
     * @return
     */
    @RecordOperation()
    @GetMapping("/getAllFood")
    public CommonResult getAllFood(String foodName,Integer foodType,Integer foodStatus){
        List<HfFoods> foodList = foodService.getAllFood(foodName,foodType,foodStatus);
        if (foodList != null){
            return new CommonResult(200,"查询成功",foodList);
        }else{
            return new CommonResult(500,"查询失败");
        }
    }

    /**
     * 添加食物
     * @param hfFoods
     * @return
     */
    @PostMapping("/saveFood")
    public CommonResult saveFood(@RequestBody HfFoods hfFoods){
        Integer integer = foodService.saveFood(hfFoods);
        if (integer ==1){
            return new CommonResult(200,"添加成功",foodService.getAllFood("",null,null));
        }
        return new CommonResult(500,"添加失败");
    }

    /**
     * 删除食物(物理删除)
     * @param id
     * @return
     */
    @DeleteMapping("/deleteFoodById")
    public CommonResult deleteFoodById(Integer id){
        Integer integer = foodService.deleteFoodById(id);
        if (integer ==1 ){
            return new CommonResult(200,"删除成功",foodService.getAllFood("",null,null));
        }
        return new CommonResult(500,"删除失败");
    }

    /**
     * 修改食物方法
     * @param hfFoods
     * @return
     */
    @PostMapping("/updateFood")
    public CommonResult updateFood(@RequestBody HfFoods hfFoods){
        Integer integer = foodService.updateFood(hfFoods);
        if (integer ==1 ){
            return new CommonResult(200,"修改成功",foodService.getAllFood("", null,null));
        }
        return new CommonResult(500,"修改失败");
    }

    /**
     * 食物上架下架
     * @param hfFoods
     * @return
     */
    @PostMapping("/updateFoodStatus")
    public CommonResult updateFoodStatus(@RequestBody HfFoods hfFoods){
        Integer integer = foodService.updateFoodStatus(hfFoods);
        if (integer ==1 ){
            return new CommonResult(200,"修改成功",foodService.getAllFood("", null,null));
        }
        return new CommonResult(500,"修改失败");
    }

    /**
     * 图片上传并返回url
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public CommonResult upload(@RequestParam("foodFile") MultipartFile file) {
        String filename = file.getOriginalFilename();
        System.out.println(filename + "==filename");
        String foodImg = null;
        try {

            if (file != null) {
                if (!"".equals(filename.trim())) {
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    file.transferTo(newFile);
                    // 上传到OSS
                    foodImg = aliyunOSSUtil.upLoad(newFile);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        System.out.println(foodImg);
        return new CommonResult(200,"上传成功",foodImg);
    }
}
